package me.send.produce.data.controller;

import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.send.business.base.Pager;
import me.send.common.poi.ExcelUtil;
import me.send.common.poi.ExcelUtil.ExcelExportData;
import me.send.data.service.pojo.BrokerPrize;
import me.send.data.service.pojo.BrokerPrizeBean;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.data.service.service.BrokerPrizeService;
import me.send.data.service.service.TreeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "data")
public class BrokerPrizeController {

	@Autowired
	private BrokerPrizeService brokerPrizeService;

	@Autowired
	private TreeService treeService;

	 @RequestMapping(value = "broker/excel/export")    
	    public void exportExcel(HttpServletRequest request, HttpServletResponse response)     
	    throws Exception { 
	//获取前台额外传递过来的查询条件  
	        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
	        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String start_time = request.getParameter("start_time"); 
	        String end_time = request.getParameter("end_time");  
	        String phone = request.getParameter("phone");  
	        String realname = request.getParameter("realname");  
	        String city = request.getParameter("city");  
	        if(phone==null||(phone!=null&&"".equals(phone.trim()))){
	        	phone=null;
	        }
	        if(realname==null||(realname!=null&&"".equals(realname.trim()))){
	        	realname=null;
	        }
	        if(city==null||(city!=null&&"".equals(city.trim()))){
	        	city=null;
	        }
	        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
	        	Date startDate=null;
	        	Date endDate=null;
	        	try {
	        		startDate=sdf.parse(start_time);
	        		start_time=format.format(startDate);
	        		endDate=sdf.parse(end_time);
	        		end_time=format.format(endDate);
	        	} catch (ParseException e) {
	        		// TODO Auto-generated catch block
	        		e.printStackTrace();
	        	}
	        }else{
	        	start_time=null;
	        	end_time=null;
	        }
	        
	        Integer count=brokerPrizeService.count(phone,realname,city,start_time,end_time); 
	        Pager page=new Pager();
	        page.setRows(count);
	        Integer p=(0/count)+1;
	        page.setPage(p);
	        
	                //随便组织的查询结果  
	        List<BrokerPrize> datalist=brokerPrizeService.list(phone,realname,city,start_time,end_time,page);
	        List<BrokerPrizeBean> list=new ArrayList<BrokerPrizeBean>();
	        for(BrokerPrize prize:datalist){
	        	BrokerPrizeBean bean=new BrokerPrizeBean();
	        	bean.setCityname(prize.getCityname());
	        	bean.setMaxEvaluateCount(prize.getMaxEvaluateCount());
	        	bean.setMinEvaluateCount(prize.getMinEvaluateCount());
	        	bean.setMidEvaluateCount(prize.getMidEvaluateCount());
	        	bean.setOrderCount(prize.getOrderCount());
	        	bean.setReal_name(prize.getReal_name());
	        	bean.setTelephone(prize.getTelephone());
	        	bean.setUsername(prize.getUsername());
	        	
	        	double amount=((int)(prize.getOrderCount()/10))*50;
	        	if(amount>500){
	        		bean.setAmount(600d);
	        	}else{
	        		bean.setAmount(amount);
	        	}
	        	list.add(bean);
	        }
	        
		 
		 	List<String[]> columNames = new ArrayList<String[]>();
	        columNames.add(new String[] { "经纪人手机号", "经纪人真实姓名", "经纪人昵称","城市", "服务单数", "差评数", "中评数", "好评数" , "预奖励金额"});

	        List<String[]> fieldNames = new ArrayList<String[]>();
	        fieldNames.add(new String[] { "telephone", "real_name", "username","cityname", "orderCount", "minEvaluateCount", "midEvaluateCount", "maxEvaluateCount", "amount" });

	        LinkedHashMap<String, List<?>> map = new LinkedHashMap<String, List<?>>();
	        map.put("经纪人服务数据", list);
	        
	        
	        ExcelExportData setInfo = new ExcelExportData();
	        setInfo.setDataMap(map);
	        setInfo.setFieldNames(fieldNames);
	        setInfo.setTitles(new String[] { "经纪人服务数据报表"});
	        setInfo.setColumnNames(columNames);
		 
		 
	        response.setContentType("application/vnd.ms-excel");    
	        response.setHeader("Content-disposition", "attachment;filename=broker_data.xls");    
	        OutputStream ouputStream = response.getOutputStream();    
	        ouputStream.write(ExcelUtil.export2ByteArray(setInfo));
	        ouputStream.flush();    
	        ouputStream.close();    
	   }    

	// 查询提现订单列表
	@RequestMapping(value = "broker/list")
	public ModelAndView withdraw_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/broker/broker_prize");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "data/broker/list"));
		modelAndView.addObject("module", "经纪人服务数据报表");
		return modelAndView;
	}

	
	@RequestMapping(value="broker/page", method=RequestMethod.GET)  
    @ResponseBody  
    public DatatablesViewPage<BrokerPrize> datatablesTest(HttpServletRequest request){  
//获取分页控件的信息  
        String start = request.getParameter("start");  
        String length = request.getParameter("length"); 
//获取前台额外传递过来的查询条件  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start_time = request.getParameter("start_time"); 
        String end_time = request.getParameter("end_time");  
        String phone = request.getParameter("phone");  
        String realname = request.getParameter("realname");  
        String city = request.getParameter("city");  
        if(phone==null||(phone!=null&&"".equals(phone.trim()))){
        	phone=null;
        }
        if(realname==null||(realname!=null&&"".equals(realname.trim()))){
        	realname=null;
        }
        if(city==null||(city!=null&&"".equals(city.trim()))){
        	city=null;
        }
        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
        	Date startDate=null;
        	Date endDate=null;
        	try {
        		startDate=sdf.parse(start_time);
        		start_time=format.format(startDate);
        		endDate=sdf.parse(end_time);
        		end_time=format.format(endDate);
        	} catch (ParseException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        	}
        }else{
        	start_time=null;
        	end_time=null;
        }
        
        Pager page=new Pager();
        Integer row=Integer.parseInt(length);
        page.setRows(row);
        Integer startInt=Integer.parseInt(start);
        Integer p=(startInt/row)+1;
        page.setPage(p);
        
                //随便组织的查询结果  
        List<BrokerPrize> list=brokerPrizeService.list(phone,realname,city,start_time,end_time,page); 
        Integer count=brokerPrizeService.count(phone,realname,city,start_time,end_time); 
        DatatablesViewPage<BrokerPrize> view = new DatatablesViewPage<BrokerPrize>();  
        view.setiTotalDisplayRecords(count);  
        view.setiTotalRecords(count);  
          
        view.setAaData(list);  
        return view;  
    }  
	
}





